package com.ww.demo.thread.d5;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 * 倒计时
 */
public class CountDownLatchDemo {

    private static final int THREAD_COUNT_NUM = 7;
    private static CountDownLatch countDownLatch = new CountDownLatch(THREAD_COUNT_NUM);

    public static void main(String[] args) {
        for(int i = 1; i <= THREAD_COUNT_NUM; i++){
            int index = i;
            new Thread(() -> {
                try {
                    Thread.sleep(new Random().nextInt(3000));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("第" + index + "龙珠已收集到!");
                countDownLatch.countDown();
            }).start();
        }
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("龙珠已集齐，召唤神龙!");
    }

}
